package com.yuexin.project.statistic.book.service;


import com.google.common.collect.Lists;
import com.yuexin.project.statistic.book.domain.Book;
import com.yuexin.project.statistic.book.domain.EatBookList;
import com.yuexin.project.statistic.book.domain.StatsBook;
import com.yuexin.project.statistic.book.formbean.EatBookFormBean;
import com.yuexin.project.statistic.book.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 报餐明细 服务层实现
 *
 * @author yuexin
 * @date 2018-10-14
 */
@Service
public class BookServiceImpl implements IBookService {
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询报餐明细信息
     *
     * @param bookId 报餐明细ID
     * @return 报餐明细信息
     */
    @Override
    public Book selectBookById(Integer bookId) {
        return bookMapper.selectBookById(bookId);
    }

    /**
     * 查询报餐明细列表
     *
     * @param book 报餐明细信息
     * @return 报餐明细集合
     */
    @Override
    public List<Book> selectBookList(Book book) {
        return bookMapper.selectBookList(book);
    }

    /**
     * 查询报餐统计列表
     *
     * @param book 报餐明细信息
     * @return 报餐明细集合
     */
    @Override
    public List<StatsBook> selectList(Book book) {
        return setBookList(bookMapper.selectList(book));
    }

    private List<StatsBook> setBookList(List<StatsBook> statsBooks) {
        if (statsBooks == null || statsBooks.isEmpty()) {
            return statsBooks;
        }
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sDateFormatHS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        statsBooks.stream().forEach(item -> {
            StringBuilder sb = new StringBuilder();
            item.getBookDishes().stream().forEach(dish -> {
                if ("1".equals(item.getTypes())) {
                    sb.append(dish.getDishName() + "*" + dish.getSeveral());
                }
            });
            item.setDishes(sb.toString());
            BigDecimal total = item.getTotalPrice() == null ? new BigDecimal(0) : item.getTotalPrice();
            int num = item.getBookNum() == null || item.getBookNum() == 0 ? 1 : item.getBookNum();
            item.setPrice(total.divide(new BigDecimal(num), 2, BigDecimal.ROUND_HALF_UP));
            item.setBookDishes(null);
            item.setEatDateExcel(item.getEatDate() != null ? sDateFormat.format(item.getEatDate()) : null);
            item.setSignTimeExcel(item.getSignTime() != null ? sDateFormatHS.format(item.getSignTime()) : null);
        });
        return statsBooks;
    }

    /**
     * 查询报餐统计列表
     *
     * @param book 报餐明细信息
     * @return 报餐明细集合
     */
    @Override
    public List<StatsBook> selecExportList(Book book) {
        return setBookList(bookMapper.selecExportList(book));
    }

    /**
     * 新增报餐明细
     *
     * @param book 报餐明细信息
     * @return 结果
     */
    @Override
    public int insertBook(Book book) {
        return bookMapper.insertBook(book);
    }

    /**
     * 修改报餐明细
     *
     * @param book 报餐明细信息
     * @return 结果
     */
    @Override
    public int updateBook(Book book) {
        return bookMapper.updateBook(book);
    }

    /**
     * 删除报餐明细对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBookByIds(String ids) {
        return bookMapper.deleteBookByIds(Lists.newArrayList(ids.split(",")));
    }

    /**
     * 获取就餐数据
     *
     * @return
     */
    @Override
    public List<EatBookList> selectEatBook(EatBookFormBean form) {
        return bookMapper.selectEatBook(form);
    }

}
